package com.knife.message.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knife.common.exception.CommonException;
import com.knife.message.app.service.KnifeMessageTemplateService;
import com.knife.message.domain.entity.KnifeMessageTemplate;
import com.knife.message.infra.cache.v2.KnifeMessageTemplateCache;
import com.knife.message.infra.mapper.KnifeMessageTemplateMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 消息模板
 *
 * @author lth
 * @created 2025年08月14日 星期四 9:01
 */
@Service
public class KnifeMessageTemplateServiceImpl implements KnifeMessageTemplateService {
    @Resource
    private KnifeMessageTemplateMapper knifeMessageTemplateMapper;
    @Resource
    private KnifeMessageTemplateCache knifeMessageTemplateCache;

    @Override
    public Page<KnifeMessageTemplate> selectPage(Page<KnifeMessageTemplate> pageRequest, KnifeMessageTemplate knifeMessageTemplate) {
        LambdaQueryWrapper<KnifeMessageTemplate> knifeMessageTemplateLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeMessageTemplateLambdaQueryWrapper
                .like(StringUtils.isNotEmpty(knifeMessageTemplate.getCode()), KnifeMessageTemplate::getCode, knifeMessageTemplate.getCode())
                .like(StringUtils.isNotEmpty(knifeMessageTemplate.getSubject()), KnifeMessageTemplate::getSubject, knifeMessageTemplate.getSubject())
                .orderByDesc(KnifeMessageTemplate::getLastUpdateDate)
        ;
        return knifeMessageTemplateMapper.selectPage(pageRequest, knifeMessageTemplateLambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public KnifeMessageTemplate saveOrUpdateTemplate(KnifeMessageTemplate knifeMessageTemplate) {
        // 先判断这个代码在库中是否存在
        LambdaQueryWrapper<KnifeMessageTemplate> knifeMessageTemplateLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeMessageTemplateLambdaQueryWrapper
                .eq(KnifeMessageTemplate::getCode, knifeMessageTemplate.getCode())
        ;
        KnifeMessageTemplate db = knifeMessageTemplateMapper.selectOne(knifeMessageTemplateLambdaQueryWrapper);
        if (Objects.isNull(db)) {
            db = new KnifeMessageTemplate();
        }
        // 替换数据
        db.setCode(knifeMessageTemplate.getCode());
        db.setSubject(knifeMessageTemplate.getSubject());
        db.setContent(knifeMessageTemplate.getContent());
        if (Objects.isNull(db.getTemplateId())) {
            knifeMessageTemplateMapper.insert(db);
        } else {
            knifeMessageTemplateMapper.updateById(db);
        }
        // 保存缓存
        knifeMessageTemplateCache.setCache(db);
        return db;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public KnifeMessageTemplate deleteTemplate(KnifeMessageTemplate knifeMessageTemplate) {
        // 先判断这个代码在库中是否存在
        LambdaQueryWrapper<KnifeMessageTemplate> knifeCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeLambdaQueryWrapper
                .eq(KnifeMessageTemplate::getCode, knifeMessageTemplate.getCode());
        KnifeMessageTemplate db = knifeMessageTemplateMapper.selectOne(knifeCodeLambdaQueryWrapper);
        if (Objects.isNull(db)) {
            throw new CommonException("删除的数据不存在");
        }
        knifeMessageTemplateMapper.deleteById(db);
        knifeMessageTemplateCache.deleteCache(db);
        return db;
    }
}
